package com.tomcov.client;



import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.tomcov.client.Ressource.TomCovRes;
import com.tomcov.client.place.AppPlaceHistoryMapper;
import com.tomcov.client.place.WelcomPlace;
import com.tomcov.client.service.JourneyPassengerService;
import com.tomcov.client.service.JourneyPassengerServiceAsync;
import com.tomcov.client.service.JourneyService;
import com.tomcov.client.service.JourneyServiceAsync;
import com.tomcov.client.service.MapService;
import com.tomcov.client.service.MapServiceAsync;
import com.tomcov.client.service.MessageService;
import com.tomcov.client.service.MessageServiceAsync;
import com.tomcov.client.service.UserService;
import com.tomcov.client.service.UserServiceAsync;


public class Tomcov implements EntryPoint {


	/** The constants. */
	private final TomCovRes constants = (TomCovRes) GWT.create(TomCovRes.class);
	
	/** The lay cont. */
	private final LayoutContainer layCont = new LayoutContainer();
	
	/** The header master panel. */
	private final ContentPanel headerMasterPanel = new ContentPanel();
	
	/** The footer master panel. */
	private final ContentPanel footerMasterPanel = new ContentPanel();
	
	/** The main master panel. */
	private final ContentPanel mainMasterPanel = new ContentPanel();

	AcceptsOneWidget headerMasterDisplay = new AcceptsOneWidget() {
		@Override
		public void setWidget(IsWidget activityWidget) {
			Widget widget = Widget.asWidgetOrNull(activityWidget);
			headerMasterPanel.setVisible(widget != null);
			if (widget != null)
				headerMasterPanel.add(widget);
			layCont.layout();

		}
	};
	AcceptsOneWidget footerMasterDisplay = new AcceptsOneWidget() {
		@Override
		public void setWidget(IsWidget activityWidget) {
			Widget widget = Widget.asWidgetOrNull(activityWidget);
			footerMasterPanel.setVisible(widget != null);
			if (widget != null)
				footerMasterPanel.add(widget);
			layCont.layout();
		}
	};
	AcceptsOneWidget mainDisplay = new AcceptsOneWidget() {
		@Override
		public void setWidget(IsWidget activityWidget) {
			Widget widget = Widget.asWidgetOrNull(activityWidget);
			mainMasterPanel.setVisible(widget != null);
			if (mainMasterPanel.getWidget(0) != null)
				mainMasterPanel.remove(mainMasterPanel.getWidget(0));
			if (widget != null)
				mainMasterPanel.add(widget);

			if (headerMasterPanel.getWidget(0) == null)
				headerMasterPanel.hide();
			if(footerMasterPanel.getWidget(0)== null)
				footerMasterPanel.hide();
			layCont.layout();
		}
	};


	/* (non-Javadoc)
	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
	 */
	public void onModuleLoad() {

		final Place defaultPlace = new WelcomPlace("Covoiturage");
		final BorderLayout layPan = new BorderLayout();
		final Viewport v = new Viewport();
		
		v.setLayout(new FitLayout());
		//v.addStyleName("appWidget");
		//layCont.addStyleName("appWidget");

		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, constants.css().northp());
		layCont.add(headerMasterPanel, northData);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		layCont.add(mainMasterPanel, centerData);
		BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH, constants.css().southp());
		layCont.add(footerMasterPanel, southData);


		mainMasterPanel.setHeaderVisible(false);
		headerMasterPanel.setHeaderVisible(false);
		footerMasterPanel.setHeaderVisible(false);
		mainMasterPanel.setScrollMode(Scroll.AUTO);
		mainMasterPanel.setBodyBorder(false);
		footerMasterPanel.setBodyBorder(false);

		UserServiceAsync USA = GWT.create(UserService.class);
		JourneyServiceAsync JSA = GWT.create(JourneyService.class);
		MessageServiceAsync MSA = GWT.create(MessageService.class);
		JourneyPassengerServiceAsync JPSA = GWT.create(JourneyPassengerService.class);
		MapServiceAsync MapSA = GWT.create(MapService.class);
		
		ClientFactory clientFactory = new ClientFactoryImpl(USA,JSA,MSA,JPSA,MapSA);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();

		ActivityMapper headerMasterActivityMapper = new HeaderAppActivityMapper(clientFactory);
		ActivityMapper mainActivityMapper = new MainAppActivityMapper(clientFactory);
		ActivityMapper footerActivityMapper = new FooterAppActivityMapper(clientFactory);

		ActivityManager headerMasterActivityManager = new ActivityManager(headerMasterActivityMapper, eventBus);
		ActivityManager mainActivityManager = new ActivityManager(mainActivityMapper, eventBus);
		ActivityManager footerActivityManager = new ActivityManager(footerActivityMapper, eventBus);

		headerMasterActivityManager.setDisplay(headerMasterDisplay);
		mainActivityManager.setDisplay(mainDisplay);
		footerActivityManager.setDisplay(footerMasterDisplay);
		mainMasterPanel.addStyleName("appWidget");

		AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);

		layCont.setLayout(layPan);
		v.add(layCont);
		RootLayoutPanel.get().add(v);

		historyHandler.handleCurrentHistory();

	}
}
